<?php

class application_installer extends class_installer
{
	/**
	 * application_installer::set_requirements
	 * 
	 * Sets the requirements for this app
	 *
	 */		
	function set_requirements()
	{
		//-----------------------------------------
		// INIT
		//-----------------------------------------
		
		$wfiles = array();
		$wdirs  = array( 0 => INS_ROOT_PATH . 'installfiles' );
		
		//-----------------------------------------
		// Basics
		//-----------------------------------------
		
		
//		$this->set_required( $dirs, $files );
		$this->version_php_min   = '4.3.0';
		
		//-----------------------------------------
		// Grab XML file and check
		//-----------------------------------------
		
		if ( file_exists( INS_ROOT_PATH . 'installfiles/writeablefiles.xml' ) )
		{
			$config = implode( '', file( INS_ROOT_PATH . 'installfiles/writeablefiles.xml' ) );
			$xml = new class_xml();
	
			$config = $xml->xml_parse_document( $config );
			
			//-----------------------------------------
			// Loop through and sort out settings...
			//-----------------------------------------

			foreach( $xml->xml_array['installdata']['file'] as $id => $entry )
			{
				if ( preg_match( "#\.\w{3,}$#si", $entry['path']['VALUE'] ) )
				{
					$wfiles[] = INS_DOC_ROOT_PATH  . $entry['path']['VALUE'];
				}
				else
				{
					$wdirs[] = INS_DOC_ROOT_PATH  . $entry['path']['VALUE'];
				}
			}
		}
		
		$this->set_writeable( $wdirs, $wfiles );
	}
	
	/**
	 * application_installer::write_configuration
	 * 
	 * Writes the configuration files for this app
	 *
	 */		
	function write_configuration()
	{
		$dir = str_replace( 'installer', '' , getcwd() );
		$dir = str_replace( 'install'  , '' , getcwd() );
		$dir = str_replace( '\\'       , '/', $dir );
		$filename = 'config.php';
		$core_conf = "<"."?php\n";
		$core_conf .='//--
		// Config file settings
		//--
		//
		//note: ture = on, false = off.
		//
		$show = true; // users can view the files in a file viewer
		$del = ' . $this->install->saved_data['del'] . '; // users can delete files
		$edit = ' . $this->install->saved_data['edit'] . '; // users can edit files
		$rename = ' . $this->install->saved_data['rename'] . '; // users can rename files
		$copy = ' . $this->install->saved_data['copy'] . '; // users can copy files
		$move = ' . $this->install->saved_data['move'] . '; // users can move files
		$upload = ' . $this->install->saved_data['upload'] . '; // users can upload files
		
		//-- Admin settings --
		include(\'includes/admin.php\');

		//-- [END] --
		
		//---[DO NOT EDIT BEOND THIS POINT]---
		$debug = true;';

		$core_conf .= "\n".'?'.'>';
		$file = "$dir$filename";

		/* Write Configuration Files */
		$output[] = 'Writing configuration files...<br />';
		
		$this->write_file( $file , $core_conf );
	}
	
	
	/**
	 * application_installer::create_admin_account
	 * 
	 * Creates the local app admin entry
	 *
	 */	
	function create_admin_account()
	{
		$dir = str_replace( 'installer', '' , getcwd() );
		$dir = str_replace( 'install'  , '' , getcwd() );
		$dir = str_replace( '\\'       , '/', $dir );
		$dir = $dir.'users/'.$this->install->saved_data[username].'.opuf';
	$password = crypt(md5($this->install->saved_data['pass_main']), md5($this->install->saved_data['username']));
$opuf ='  - User Password And Access Info -
Password="'.$password.'"
Power="Administrator"
 - User Settings -
Home="'.$this->install->saved_data['home_dir'].'"
trashdir="'.$this->install->saved_data['trash_dir'].'"
trashfile="'.$this->install->saved_data['trash_file'].'"
 - Login Information -
LoginAttempts="0"
LastLogin="00.00.00, 00:00:00"
IP="0.0.0.0"
----------------
oPanel User File';
		/* Write User File */
		$output[] = 'Creating Administrative user account...<br />';
		$this->write_file( $file , $opuf );
	}

	/**
	 * application_installer::create_license_file
	 * 
	 * Creates the local app license file
	 *
	 */	
	function create_license_file($code)
	{
		$dir = str_replace( 'installer', '' , getcwd() );
		$dir = str_replace( 'install'  , '' , getcwd() );
		$dir = str_replace( '\\'       , '/', $dir );
		$filename = 'license.oplf';
		$file = "$dir$filename";
		/* Write license File */
		$output[] = 'Writing oPanel license file...<br />';
		$this->write_file( $file , $code );
	}
}
?>